<?php

namespace model;

/**
 * Description of MyExcelDocument
 *
 * @author Damian
 */
class MyExcelDocument extends \PHPExcel {

    private $style;
    
    public function __construct(&$user, TblShop &$shop) {
        parent::__construct();
        $this->setAllProperties($user, $shop);
        $this->createStyle();
    }

    public function setAllProperties(&$user, TblShop &$shop) {
        if ($user){
             $this->getProperties()->setCreator($user->Name . " " . $user->Surname);
        }       
                $this->getProperties()->setTitle("Lista produktów ze sklepu " . $shop->Name)
                ->setSubject("Lista produktów ze sklepu " . $shop->Name);
    }

    public function createStyle() {
        $this->style = new \PHPExcel_Style();
        $this->style->applyFromArray(
                array(
                    'font' => array(
                        'name' => 'Arial',
                        'bold' => true,
                        'italic' => false,
                        'underline' => \PHPExcel_Style_Font::UNDERLINE_DOUBLE,
                        'strike' => false,
                        'color' => array(
                            'rgb' => '808080'
                        )
                    ),
                    'borders' => array(
                        'bottom' => array(
                            'style' => \PHPExcel_Style_Border::BORDER_DASHDOT,
                            'color' => array(
                                'rgb' => '808080'
                            )
                        ),
                        'top' => array(
                            'style' => \PHPExcel_Style_Border::BORDER_DASHDOT,
                            'color' => array(
                                'rgb' => '808080'
                            )
                        )
                    ),
                    'quotePrefix' => true,
                    'fill' => array(
                        'type' => \PHPExcel_Style_Fill::FILL_SOLID,
                        'color' => array('argb' => 'FFCCFFCC')
                    )
                )
        );
        $this->getActiveSheet()->setSharedStyle($this->style, "A1:G1");
    }

    public function save($filename) {
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
        header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
        $objWriter = \PHPExcel_IOFactory::createWriter($this, 'Excel2007');
        $objWriter->save('php://output');
        exit;
    }

}
